'/
' GUI Library
' Written by Anton Berlin, 2010.
' 
' =========================================================================
' 
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU Lesser General Public License as published
' by the Free Software Foundation; either version 3 of the License.
' 
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
' 
' You should have received a copy of the GNU Lesser General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'/

superstrict

import "GUI_Gadget.bmx"
import "GUI_Event.bmx"
import "GUI_Def.bmx"

type GUI_Button extends GUI_Gadget

	'// -------------------------------------------------------------------------------------------------------------
	'// Button states.
	'// -------------------------------------------------------------------------------------------------------------
	global STATE_IDLE:byte = 0;
	global STATE_PRESSED:byte = 1;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Update this gadget.
	'// -------------------------------------------------------------------------------------------------------------
	method update()
	
		if (not mouseHovering()) ..
			m_state = STATE_IDLE;
		
		if (m_state = STATE_PRESSED) then
		
			if (not mouseDown(1)) then
			
				m_state = STATE_IDLE;
				m_event = GUI_Event.create(GUI_Event.EVENT_INVOKE_METHOD, m_methodHook, m_hookedObject);
			
			end if
		
		end if
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Draw this gadget.
	'// -------------------------------------------------------------------------------------------------------------
	method draw()
	
		if (not m_visible) ..
			return;
		
		'// Setup the default drawing settings.
		drawutil.setColorInit(m_primitiveR, m_primitiveG, m_primitiveB);
		drawutil.setAlphaInit(m_transparency);
		drawutil.setFontInit(null);
		
		'// Draw the design of the gadget.
		design();
		
		'// Reset drawing settings.
		drawutil.resetColor();
		drawutil.resetAlpha();
		drawutil.resetFont();
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Draw the design of this gadget.
	'// -------------------------------------------------------------------------------------------------------------
	method design()
	
		'// Prepare colors.
		local color1R:int;
		local color1G:int;
		local color1B:int;
		
		local color2R:int;
		local color2G:int;
		local color2B:int;
		
		local oldPrimitiveR:short = m_primitiveR;
		local oldPrimitiveG:short = m_primitiveG;
		local oldPrimitiveB:short = m_primitiveB;
		
		local oldTextR:short = m_textColorR;
		local oldTextG:short = m_textColorG;
		local oldTextB:short = m_textColorB;
		
		if (not m_active) then
		
			m_primitiveR :* 0.75;
			m_primitiveG :* 0.75;
			m_primitiveB :* 0.75;
			
			m_textColorR = 255 - m_textColorR;
			m_textcolorG = 255 - m_textColorG;
			m_textcolorB = 255 - m_textColorB;
		
		end if
		
		if (m_state = STATE_PRESSED) then
		
			color1R = m_primitiveR * 0.5;
			color1G = m_primitiveG * 0.5;
			color1B = m_primitiveB * 0.5;
			
			color2R = m_primitiveR * 1.5;
			color2G = m_primitiveG * 1.5;
			color2B = m_primitiveB * 1.5;
		
		else
		
			color1R = m_primitiveR * 1.5;
			color1G = m_primitiveG * 1.5;
			color1B = m_primitiveB * 1.5;
			
			color2R = m_primitiveR * 0.5;
			color2G = m_primitiveG * 0.5;
			color2B = m_primitiveB * 0.5;
		
		end if
		
		'// Shadow.
		if (m_useSurroundingShadows) ..
			drawSurroundingShadow();
		
		'// Draw the primitive of the gadget.
		setAlpha(m_transparency * 1.0);
		setColor(m_primitiveR, m_primitiveG, m_primitiveB);
		drawRect(m_posX, m_posY, m_width, m_height);
		
		'// Border.
		setColor(color1R, color1G, color1B);
		drawLine(m_posX, m_posY, m_posX + m_width, m_posY, false);
		drawLine(m_posX, m_posY, m_posX, m_posY + m_height, false);
		setColor(color2R, color2G, color2B);
		drawLine(m_posX, m_posY + m_height, m_posX + m_width, m_posY + m_height, false);
		drawLine(m_posX + m_width, m_posY, m_posX + m_width, m_posY + m_height, false);
		
		'// ------------ Button Text -----------
		
		setImageFont(m_font);
		
		local x:int = m_posX + m_width / 2 - textWidth(m_text) / 2;
		local y:int = m_posY + m_height / 2 - textHeight(m_text) / 2 - 1;
		
		local offset:short = 0;
		
		if (m_state = STATE_PRESSED) ..
			offset = 1;
		
		'// Shadow.
		if (m_useTextShadows) then
		
			setAlpha(m_transparency * 0.5);
			setColor(0, 0, 0);
			drawText(m_text, x + offset + 3, y + offset + 3);
		
		end if
		
		if (m_useTextBorders) then
		
			'// Upper border.
			setAlpha(m_transparency * 1.0);
			setColor(m_textColorR * 0.1, m_textColorG * 0.1, m_textColorB * 0.1)
			drawText(m_text, x + offset, y + offset);
			setAlpha(m_transparency * 1.0);
			setColor(m_textColorR * 0.1, m_textColorG * 0.1, m_textColorB * 0.1)
			drawText(m_text, x + offset, y + offset + 1);
			setColor(m_textColorR * 0.1, m_textColorG * 0.1, m_textColorB * 0.1)
			drawText(m_text, x + offset + 1, y + offset);
			
			'// Lower border.
			setColor(m_textColorR * 0.1, m_textColorG * 0.1, m_textColorB * 0.1)
			drawText(m_text, x + offset + 2, y + offset + 2);
		
		end if
		
		'// Main text.
		setColor(m_textColorR, m_textColorG, m_textColorB);
		drawText(m_text, x + offset + 1, y + offset + 1);
		
		if (not m_active) then
		
			m_primitiveR = oldPrimitiveR;
			m_primitiveG = oldPrimitiveG;
			m_primitiveB = oldPrimitiveB;
			
			m_textColorR = oldTextR;
			m_textcolorG = oldTextG;
			m_textcolorB = oldTextB;
		
		end if
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Set the button text.
	'// -------------------------------------------------------------------------------------------------------------
	method setText(str:string)
	
		m_text = str;
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Set the color of the button text.
	'// -------------------------------------------------------------------------------------------------------------
	method setTextColor(r:byte, g:byte, b:byte)
	
		m_textColorR = r;
		m_textColorG = g;
		m_textColorB = b;
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Set whether or not to use shadows on the button text.
	'// -------------------------------------------------------------------------------------------------------------
	method setTextShadowUsage(value:byte)
	
		m_useTextShadows = value;
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Set whether or not to use borders on the button text.
	'// -------------------------------------------------------------------------------------------------------------
	method setTextBorderUsage(value:byte)
	
		m_useTextBorders = value;
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Set the font of the button text.
	'// -------------------------------------------------------------------------------------------------------------
	method setFont(font:TImageFont)
	
		m_font = font;
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Press the button.
	'// -------------------------------------------------------------------------------------------------------------
	method press()
	
		if (mouseDown(1)) ..
			m_state = STATE_PRESSED;
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// The button text to use.
	'// -------------------------------------------------------------------------------------------------------------
	field m_text:string;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Flag that determines whether or not to draw shadows on the button text.
	'// -------------------------------------------------------------------------------------------------------------
	field m_useTextShadows:byte = false;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Flag that determines whether or not to draw borders on the button text.
	'// -------------------------------------------------------------------------------------------------------------
	field m_useTextBorders:byte = false;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// The font to use on the button text.
	'// -------------------------------------------------------------------------------------------------------------
	field m_font:TImageFont;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Button text color.
	'// -------------------------------------------------------------------------------------------------------------
	field m_textColorR:byte;
	field m_textColorG:byte;
	field m_textColorB:byte;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Current mouse state
	'// -------------------------------------------------------------------------------------------------------------
	field m_state:byte = STATE_IDLE;

end type

